package com.byxy.managerial.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.byxy.managerial.entity.Department;
import com.byxy.managerial.service.DepartmentService;
import com.opensymphony.xwork2.ActionSupport;

public class DepartmentUpdateAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String dname;
	private String principal;
	private String depint;
	private Department depart;

	@Resource
	private DepartmentService departmentService;
	
	public String getDepartment() {
		depart = departmentService.getById(id);
		System.err.println(depart.getDepint());
		return SUCCESS;
	}

	public void updateDepart() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Map<String, Object> map = new HashMap<String, Object>();
		Department department = new Department(dname, depint, principal);	
		department.setId(id);//唯一id进入判断,防止修改后出现重复部门
		boolean isUpdate = departmentService.update(department);
		if (isUpdate) {
			map.put("isUpdate", isUpdate);
		} else {
			map.put("isUpdate", "该部门名称已存在！");
		}
		PrintWriter pw = response.getWriter();
		String str = JSON.toJSONString(map);
		pw.write(str);
		pw.flush();
		pw.close();
	}

	public String getDname() {
		return dname;
	}

	public void setDname(String dname) {
		this.dname = dname;
	}

	public String getPrincipal() {
		return principal;
	}

	public void setPrincipal(String principal) {
		this.principal = principal;
	}

	public String getDepint() {
		return depint;
	}

	public void setDepint(String depint) {
		this.depint = depint;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Department getDepart() {
		return depart;
	}

	public void setDepart(Department depart) {
		this.depart = depart;
	}
	
	

}
